<template>
  <div class="cart-comtainer">
    <div class="goods-group">
      <div class="mui-card" v-for="item in cart" :key="item.id">
        <!--内容区-->
        <div class="mui-card-content">
          <div class="mui-card-content-inner goods-item">
            <mt-switch
              v-model="selectObj[item.id]"
              @change="selectedChange(item.id,selectObj[item.id])"
            ></mt-switch>
            <img :src="item.thumb_path" alt />
            <div class="info">
              <h3 class="title">{{item.title}}</h3>
              <div class="price-group">
                <span class="price">¥ {{item.sell_price}}</span>
                <mt-nobox :init="idAndCount[item.id]" :id="item.id" />
                <span class="del" @click="deleteCart(item.id)">删除</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->

    <div class="mui-card">
      <!--内容区-->
      <div class="mui-card-content payment">
        <div class="pay-info">
          <div>总计 ( 不含邮费 )</div>
          <div class="divrice">
            已勾选商品
            <span class="num">{{totalCount}}</span> 件, 总价
            <span class="total">¥ {{totalPrice}}</span>
          </div>
        </div>
        <div class="calc">
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import MtNobox from "../sub-components/cart_nobox.vue";
import { mapGetters, mapMutations } from "vuex";
export default {
  components: {
    MtNobox
  },
  created() {
    this.__getCartList();
  },
  data() {
    return {
      value: true,
      cart: []
    };
  },
  methods: {
    ...mapMutations(["deleteFormCart", "selectedChange"]),
    // =========== 发送请求 ===========
    async __getCartList() {
      if (!this.goods_id.length) return;
      const { data: res } = await this.$http.get(
        "goods/getshopcarlist/" + this.goods_id
      );
      if (res.status !== 0) return;
      this.cart = res.message;
    },
    deleteCart(id) {
      this.cart = this.cart.filter(el => el.id !== id);
      this.deleteFormCart(id);
    },
    selectedChange(id, selected) {
      console.log(id, selected);
      this.$store.commit("selectedChange", { id, selected });
    }
  },
  computed: {
    ...mapGetters(["goods_id", "idAndCount", "selectObj", "totalCount",'totalPrice'])
  }
};
</script>

<style scoped lang='scss'>
.cart-comtainer {
  background-color: #eee;
  overflow: hidden;
  .goods-group {
    .goods-item {
      display: flex;
      img {
        width: 60px;
        height: 60px;
      }
      .info {
        display: flex;

        flex-direction: column;
        .title {
          font-size: 13px;
        }
        justify-content: space-between;
        .price-group {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .price {
            font-size: 16px;
            color: crimson;
            font-weight: 700;
          }
          .del {
            color: cornflowerblue;
          }
        }
      }
    }
  }
  .payment {
    display: flex;
    padding: 15px;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    color: #999;
    .divrice {
      padding-top: 8px;

      .num,
      .total {
        font-size: 15px;
        color: red;
        font-weight: 700;
      }
    }
  }
}
</style>>